<?php
class Page
{
	private $_Article;
	
	public function __construct()
	{
		$this->_Article = new Zend_Db_Table('pages');
		

	}

    public function getAll()
    {
         $Rows = $this->_Article->fetchAll($this->_Article->select()->order('Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    
    public function getAllByName($filter)
    {
         $Rows = $this->_Article->fetchAll($this->_Article->select()->where(' LOWER(Title) LIKE LOWER("%' . mysql_escape_string( $filter ). '%")' )->order('Date'));
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }
    
    
   public function getByCategory($CID)
    {
        $select = $this->_Article->select()->where("CategoryID = ?", $CID)->order('Date');
        $Rows = $this->_Article->fetchAll($select);
         if ($Rows)
         {
            return $Rows->toArray();
         }
         return array();
    }

    
    public function Add($Title,$Text)
    {
        $Data = array('Title'=>$Title,'Text'=>$Text); 
        $Row = $this->_Article->createRow($Data);
        
        $Row->save();
        
        $Doc = array();
         
        $DocField['Type']  = 'Text';
        $DocField['Key']   = 'Text';
        $DocField['Data']  = $Text;
        $Doc[] = $DocField;

         
        $DocField['Type']  = 'Text';
        $DocField['Key']   = 'Title';
        $DocField['Data']  = $Title;
        $Doc[] = $DocField;

        $DocField['Type']  = 'UnIndexed';
        $DocField['Key']   = 'AID';
        $DocField['Data']  = $Row->ID;;
        $Doc[] = $DocField; 
        
//        $Lucene = new Lucene();
//        $Lucene->AddDocument($Doc);
        
        return  $Row->ID;
    }

    public function Edit($ID,$Title = '',$Text = '')
    {
        $Row = $this->_Article->find($ID)->current();
        $Row->Text  = $Text;
        $Row->Title = $Title;  
        $Row->save();
    }
    
    public function Remove($ID)
    {
        $this->_Article->find($ID)->current()->delete();
    } 
    
    public function getOne($ID)
    {
        $Row = $this->_Article->find($ID)->current();
        if ($Row) return $Row->toArray();
        return false;
    }
    
    
    public function setCategory($ID, $categoryId)
    {
        $Row = $this->_Article->find($ID)->current();
        if (!$Row) return ;
        
        $Row->CategoryID = $categoryId;
        $Row->save(); 
        
        return false;
    }
    
    public function getSearchIndex()
    {
         $Rows = $this->_Article->fetchAll($this->_Article->select()->order('Date'));
         $result = array();
         if ($Rows)
         {
            foreach ($Rows as $row)
            {
                $route = array('module'=>'page','controller'=>'index','action'=>'view','title'=>Transliteration::asURLSegment($row['Title']),'id'=>$row['ID'],'route'=>'articleFURL');
                $result[] = array('title'=>$row['Title'], 'text'=>$row['Text'], 'teaser'=>mb_substr($row['Text'], 0, strpos($row['Text'], '.')+1),'image'=>'',  'route'=>serialize($route), 'type'=>'page', 'tag'=>$row['ID']);
            }
            
            return $result;
         }
         return array();
    }
    
    
}